<template>
  <div class="app-container">
    <div>
      <!-- 条件栏 -->
      <PageFilter
        v-model:query="filterInfo.query"
        :filter-list="filterInfo.list"
      >
        <template v-slot:filter-fiscalPeriodStart>
          <el-input
            v-model="filterInfo.query.fiscalPeriodStart"
            class="not-mongolia"
            type="input"
            :required="true"
            :disabled="true"
            :placeholder="'请选择会计期间'"
            style="width: 180px"
          >
            <template #append>
              <el-icon @click="checkFiscalPeriodStart()"
                ><MoreFilled
              /></el-icon>
            </template>
          </el-input>
          ~
          <el-input
            v-model="filterInfo.query.fiscalPeriodEnd"
            class="not-mongolia"
            type="input"
            :required="true"
            :disabled="true"
            style="width: 180px"
            :placeholder="'请选择会计期间'"
          >
            <template #append>
              <el-icon @click="checkFiscalPeriodEnd()"><MoreFilled /></el-icon>
            </template>
          </el-input>
        </template>
      </PageFilter>

      <!-- 表格 -->
      <TableContainer
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        :pageTotal="pageInfos.pageTotal"
        v-model:data="tableInfo.data"
        :tab-index="false"
        :check-box="false"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :api="tableInfo.api"
        :pager="false"
        :summary="true"
        :summary-field-list="tableInfo.summaryFieldList"
        :isDynamic="false"
      >
        <template v-slot:col-changeInAmount="scope">
          <span
            v-if="scope.row.changeInAmount > 0"
            class="slip-edit"
            @click="toWarehouseAffair(scope.row, 'in')"
            >{{ $fn.TripartiteMethod(scope.row.changeInAmount)  }}</span
          >
          <span v-else>{{ $fn.TripartiteMethod(scope.row.changeInAmount) }}</span>
        </template>
        <template v-slot:col-changeOutAmount="scope">
          <span
            v-if="scope.row.changeOutAmount > 0"
            class="slip-edit"
            @click="toWarehouseAffair(scope.row, 'out')"
            >{{ $fn.TripartiteMethod(scope.row.changeOutAmount)  }}</span
          >
          <span v-else>{{ $fn.TripartiteMethod(scope.row.changeOutAmount) }}</span>
        </template>
      </TableContainer>
    </div>
    <SelectFiscalPeriod
      v-if="selectFiscalPeriodInfo.startVisible"
      v-model:show-dialog="selectFiscalPeriodInfo.startVisible"
      :save-empty="true"
      :check-box="false"
      :highlight-row="true"
      :query="selectFiscalPeriodInfo.startQuery"
      @callback="callBackSelectFiscalPeriodStart"
    />
    <SelectFiscalPeriod
      v-if="selectFiscalPeriodInfo.endVisible"
      v-model:show-dialog="selectFiscalPeriodInfo.endVisible"
      :save-empty="true"
      :check-box="false"
      :highlight-row="true"
      :query="selectFiscalPeriodInfo.endQuery"
      @callback="callBackSelectFiscalPeriodEnd"
    />
  </div>
</template>

<script setup name="purchaseSalesAffairStock">
import { fatchTemplateByCode } from '@/api/base'
import {
  selectPurchaseSalesAffairList,
  exportPurchaseSalesAffairStock,
  printPurchaseSalesAffairStock,
} from '@/api/dataQuery.js'
const { proxy } = getCurrentInstance()
const operateTime = ref([])
const filterInfo = ref({
  query: {
    inventoryCategories: 1,
    fiscalPeriodStart: '',
    fiscalPeriodEnd: '',
  },
  list: [
    {
      type: 'select-model',
      label: '库存类别',
      value: 'inventoryCategories',
      code: 'inventory_categories',
      event: search,
    },
    {
      label: '操作时间',
      value: 'fiscalPeriodStart',
      type: 'slot',
    },
    { type: 'button', label: '搜索', event: search },
    { type: 'button', label: '打印', event: print, show: proxy.$permission('purchaseSalesAffairStock:print') },
    {
      type: 'button',
      label: '导出excel',
      event: exportExcel,
      show: proxy.$permission('purchaseSalesAffairStock:excel'),
    },
  ],
})
const selectFiscalPeriodInfo = ref({
  startVisible: false,
  endVisible: false,
  startQuery: { fiscalType: 1, startDateEqAndLes: '' },
  endQuery: { fiscalType: 1, startDateEqAndGre: '' },
})
const timeInfo = ref({
  startDate: '',
  endDate: ''
})
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  selections: [],
  summaryFieldList: [
    { value: 'beforeMonthAmount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'monthInAmount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'monthOutAmount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'changeInAmount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'changeOutAmount', formatter: proxy.$fn.TripartiteMethod },
    { value: 'currentMonthAmount', formatter: proxy.$fn.TripartiteMethod },
  ],
  fieldList: [
    { label: '类别', value: 'financialAccountValue', },
    { label: '本月结存', value: 'beforeMonthAmount', type: 'column' },
    { label: '购入', value: 'monthInAmount', type: 'column',formatter: proxy.$fn.TripartiteMethod },
    { label: '发出', value: 'monthOutAmount', type: 'column' ,formatter: proxy.$fn.TripartiteMethod},
    { label: '调整入', value: 'changeInAmount', type: 'slot' },
    { label: '调整出', value: 'changeOutAmount', type: 'slot' },
    { label: '结存', value: 'currentMonthAmount', type: 'column',formatter: proxy.$fn.TripartiteMethod },
  ],
})
const pageInfos = ref({
  pageSize: 20,
  curPage: 1,
  pageTotal: 0,
})
function getList() {
  if (
    !(
      filterInfo.value.query.fiscalPeriodEnd &&
      filterInfo.value.query.fiscalPeriodStart
    )
  ) {
    proxy.$message({
      message: `请选择会计期间`,
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  tableInfo.value.api = selectPurchaseSalesAffairList
  tableInfo.value.refresh = Math.random()
}
function search() {
  if (
    !(
      filterInfo.value.query.fiscalPeriodEnd &&
      filterInfo.value.query.fiscalPeriodStart
    )
  ) {
    proxy.$message({
      message: `请选择会计期间`,
      type: 'warning',
      duration: 3500,
      showClose: true,
    })
    return
  }
  tableInfo.value.api = selectPurchaseSalesAffairList
  filterInfo.value.query.inventoryCategories =
    filterInfo.value.query.inventoryCategories &&
    parseInt(filterInfo.value.query.inventoryCategories)
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function checkFiscalPeriodStart() {
  selectFiscalPeriodInfo.value.startVisible = true
}
function checkFiscalPeriodEnd() {
  selectFiscalPeriodInfo.value.endVisible = true
}
function callBackSelectFiscalPeriodEnd(data) {
  if (data && data.id) {
    selectFiscalPeriodInfo.value.startQuery.startDateEqAndLes = data.startDate
    filterInfo.value.query.fiscalPeriodEnd = data.name
    filterInfo.value.query.fiscalPeriodStart = filterInfo.value.query
      .fiscalPeriodStart
      ? filterInfo.value.query.fiscalPeriodStart
      : data.name
      timeInfo.value.endDate = data.endDate + ' 23:59:59'  
      timeInfo.value.startDate = timeInfo.value.startDate ? timeInfo.value.startDate:   data.startDate + ' 00:00:00'
  } else {
    filterInfo.value.query.fiscalPeriodEnd = ''
    selectFiscalPeriodInfo.value.startQuery.startDateEqAndLes = ''
    timeInfo.value.endDate = ''
  }
}
function callBackSelectFiscalPeriodStart(data) {
  if (data && data.id) {
    filterInfo.value.query.fiscalPeriodStart = data.name
    filterInfo.value.query.fiscalPeriodEnd = filterInfo.value.query
      .fiscalPeriodEnd
      ? filterInfo.value.query.fiscalPeriodEnd
      : data.name
    selectFiscalPeriodInfo.value.endQuery.startDateEqAndGre = data.startDate
    timeInfo.value.startDate = data.startDate + ' 00:00:00'
    timeInfo.value.endDate = timeInfo.value.endDate ? timeInfo.value.endDate:   data.endDate + ' 23:59:59'
  } else {
    filterInfo.value.query.fiscalPeriodStart = ''
    selectFiscalPeriodInfo.value.endQuery.startDateEqAndGre = ''
    timeInfo.value.startDate = ''
  }
}
//打印
function print() {
  fatchTemplateByCode({
    resourceCode: 'purchaseSalesStock:print',
    isDefault: 1,
  }).then((res) => {
    if (res && res.success) {
      let temp = proxy.$fn.getPrintTemp(res.data[0])
      getPrintData(temp)
    }
  })
}
function getPrintData(temp) {
  let params = {
    ...filterInfo.value.query,
  }
  printPurchaseSalesAffairStock(params).then((res) => {
    if (res.success && res) {
      let data = proxy.$fn.deepClone(res.data)
      let newData = data.map((item) => {
        return {
          ...item,
          ...item.orderInfo,
        }
      })
      proxy.$lodop.preview(temp, newData)
    }
  })
}
function exportExcel() {
  exportPurchaseSalesAffairStock(
    proxy.$fn.delEmptyAttribute(filterInfo.value.query)
  ).then((resp) => {
    if (resp) {
      proxy.$fn.parseExcel(resp)
    }
  })
}
function toWarehouseAffair(row, str){
  let url = '/dataQuery/warehouseAffair'
  let obj = {
    in: '42, 9, 7, 43, 44, 45, 46, 31, 42, 34',
    out: '51, 6, 47, 48, 49, 50, 51, 20, 18, 58'
  }
  let name = ''
  let routes = proxy.$store.state.permission.routes
  routes.map((item) => {
    if (item.children && item.children.length) {
      item.children.map((i) => {
        if (i.url === url) {
          name = url
        }
      })
    }
  })
 console.log(row.financialAccount)
  if (name) {
    proxy.$router.push({
    name: 'warehouseAffair',
    params: {
      query: JSON.stringify({
        queryMap: {
         type: {
          queryType: 'IN',
          value: obj[str]
         },
         financialAccount: {
          queryType: 'IN',
          value: row.financialAccount
         },
         inventoryCategories: {
          queryType: 'IN',
          value: filterInfo.value.query.inventoryCategories
         },
          operateTime: {
            queryType: 'BETWEEN',
            value: timeInfo.value.startDate + ',' +timeInfo.value.endDate,
          },
          
        },
      }),
    },
  })
  } else {
    proxy.$message({
      duration: 3500,
      type: 'warning',
      showClose: true,
      message: '你没有相应的菜单权限',
    })
  }
}
</script>

<style scoped></style>
